package vn.edu.saigontech.ncms.web.offering;

import java.util.ArrayList;
import java.util.Collection;

import vn.edu.saigontech.ncms.model.enumeration.TeacherType;
import vn.edu.saigontech.ncms.model.info.Offering;
import vn.edu.saigontech.ncms.model.info.OfferingMember;
import vn.edu.saigontech.ncms.model.info.Teacher;
import vn.edu.saigontech.ncms.model.studying.ClassSituation;
import vn.edu.saigontech.ncms.model.studying.EvaluateTeacher;
import vn.edu.saigontech.ncms.model.studying.Opinion;
import vn.edu.saigontech.ncms.model.studying.Suggestion;
import vn.edu.saigontech.ncms.service.ClassSituationService;
import vn.edu.saigontech.ncms.service.EvaluateTeacherService;
import vn.edu.saigontech.ncms.service.OfferingMemberService;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.service.OpinionService;
import vn.edu.saigontech.ncms.service.SuggestionService;
import vn.edu.saigontech.ncms.service.TeacherService;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class OpinionAction extends BaseAction {
	
	private Long id;
	private String teacher;
	private String methodology;
	private String skills;
	private String comment;
	private String content;
	private Long offeringId;
	
	private String date;
	private Long memberId;
	private String situation;
	private String progress;
	
	public String list() {
		REQUEST().setAttribute("offering", OfferingService.INSTANCE.findById(this.offeringId));
		return SUCCESS;
	}
	
	public String showAddEvaluateTeacher() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		Collection<Teacher> mainTeachers = offering.getInstructors();
		Collection<Teacher> teachers = TeacherService.INSTANCE.findActiveByTeacherType(TeacherType.INSTRUCTOR);
		Collection<Teacher> subTeachers = new ArrayList<Teacher>(teachers);
		subTeachers.removeAll(mainTeachers);
		
		REQUEST().setAttribute("offering", offering);
		REQUEST().setAttribute("mainTeachers", mainTeachers);
		REQUEST().setAttribute("subTeachers", subTeachers);	
		return SUCCESS;
	}
	
	public String addEvaluateTeacher() {
		EvaluateTeacher evaluateTeacher = new EvaluateTeacher();
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		evaluateTeacher.setOffering(offering);
		evaluateTeacher.setTeacher(this.teacher);
		evaluateTeacher.setMethodology(this.methodology);
		evaluateTeacher.setSkills(this.skills);
		evaluateTeacher.setComment(this.comment);
		EvaluateTeacherService.INSTANCE.persist(evaluateTeacher);
		
		this.addLoggingDetail(BaseAction.ADD, "logging.evaluation.add", this.teacher, offering.getName());
		return SUCCESS;
	}
	
	public String showEditEvaluateTeacher() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		Collection<Teacher> mainTeachers = offering.getInstructors();
		Collection<Teacher> teachers = TeacherService.INSTANCE.findActiveByTeacherType(TeacherType.INSTRUCTOR);
		Collection<Teacher> subTeachers = new ArrayList<Teacher>(teachers);
		subTeachers.removeAll(mainTeachers);
		
		REQUEST().setAttribute("model", EvaluateTeacherService.INSTANCE.findById(this.id));
		REQUEST().setAttribute("offering", offering);
		REQUEST().setAttribute("mainTeachers", mainTeachers);
		REQUEST().setAttribute("subTeachers", subTeachers);
		
		return SUCCESS;
	}
	
	public String editEvaluateTeacher() {
		EvaluateTeacher evaluateTeacher = EvaluateTeacherService.INSTANCE.findById(this.id);
		String teacherName = evaluateTeacher.getTeacher();
		evaluateTeacher.setTeacher(this.teacher);
		evaluateTeacher.setMethodology(this.methodology);
		evaluateTeacher.setSkills(this.skills);
		evaluateTeacher.setComment(this.comment);
		EvaluateTeacherService.INSTANCE.persist(evaluateTeacher);
		
		this.addLoggingDetail(BaseAction.EDIT, "logging.evaluation.edit", teacherName, evaluateTeacher.getOffering().getName());
		return SUCCESS;
	}
	
	public String deleteEvaluateTeacher() {
		EvaluateTeacher evaluate = EvaluateTeacherService.INSTANCE.findById(this.id);
		String offeringName = evaluate.getOffering().getName();
		String teacherName = evaluate.getTeacher();
		EvaluateTeacherService.INSTANCE.deleteById(this.id);
		
		this.addLoggingDetail(BaseAction.DELETE, "logging.evaluation.delete", teacherName, offeringName);
		return SUCCESS;
	}
	
	public String showAddComment() {
		REQUEST().setAttribute("offering", OfferingService.INSTANCE.findById(this.offeringId));
		return SUCCESS;
	}
	
	public String addComment() {
		Opinion opinion = new Opinion();
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		opinion.setOffering(offering);
		opinion.setContent(this.content);
		OpinionService.INSTANCE.persist(opinion);
		
		this.addLoggingDetail(BaseAction.ADD, "logging.assessment.add", offering.getName());
		return SUCCESS;
	}
	
	public String showEditComment() {
		REQUEST().setAttribute("offering", OfferingService.INSTANCE.findById(this.offeringId));
		REQUEST().setAttribute("model", OpinionService.INSTANCE.findById(this.id));
		return SUCCESS;
	}
	
	public String editComment() {
		Opinion opinion = OpinionService.INSTANCE.findById(this.id);
		opinion.setContent(this.content);
		OpinionService.INSTANCE.persist(opinion);
		
		this.addLoggingDetail(BaseAction.EDIT, "logging.assessment.edit", opinion.getOffering().getName());
		return SUCCESS;
	}
	
	public String deleteComment() {
		Opinion opinion = OpinionService.INSTANCE.findById(this.id);
		String offeringName = opinion.getOffering().getName();
		OpinionService.INSTANCE.deleteById(this.id);
		
		this.addLoggingDetail(BaseAction.DELETE, "logging.assessment.delete", offeringName);
		return SUCCESS;
	}
	
	public String showAddSuggestion() {
		REQUEST().setAttribute("offering", OfferingService.INSTANCE.findById(this.offeringId));
		return SUCCESS;
	}
	
	public String addSuggestion() {
		Suggestion suggestion = new Suggestion();
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		suggestion.setOffering(offering);
		suggestion.setContent(this.content);
		SuggestionService.INSTANCE.persist(suggestion);
		
		this.addLoggingDetail(BaseAction.ADD, "logging.suggestion.add", offering.getName());
		return SUCCESS;
	}
	
	public String showEditSuggestion() {
		REQUEST().setAttribute("offering", OfferingService.INSTANCE.findById(this.offeringId));
		REQUEST().setAttribute("model", SuggestionService.INSTANCE.findById(this.id));
		return SUCCESS;
	}
	
	public String editSuggestion() {
		Suggestion suggestion = SuggestionService.INSTANCE.findById(this.id);
		suggestion.setContent(this.content);
		SuggestionService.INSTANCE.persist(suggestion);
		
		this.addLoggingDetail(BaseAction.EDIT, "logging.suggestion.edit", suggestion.getOffering().getName());
		return SUCCESS;
	}
	
	public String deleteSuggestion() {
		Suggestion suggestion = SuggestionService.INSTANCE.findById(this.id);
		String offeringName = suggestion.getOffering().getName();
		SuggestionService.INSTANCE.deleteById(this.id);
		
		this.addLoggingDetail(BaseAction.DELETE, "logging.suggestion.delete", offeringName);
		return SUCCESS;
	}
	
	public String showAddClassSituation() {
		REQUEST().setAttribute("members", OfferingMemberService.INSTANCE.findByOfferingId(this.offeringId));
		return SUCCESS;
	}
	
	public String addClassSituation() {
		ClassSituation situation = new ClassSituation();
		OfferingMember member = OfferingMemberService.INSTANCE.findById(this.memberId);
		situation.setOfferingMember(member);
		situation.setDate(DateUtil.fromString(this.date, this.i18n("format.date")));
		situation.setSituation(this.situation);
		situation.setComment(this.comment);
		situation.setProgress(this.progress);
		
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		situation.setOffering(offering);
		
		situation = ClassSituationService.INSTANCE.persist(situation);
		
		return SUCCESS;
	}
	
	public String showEditClassSituation() {
		ClassSituation situation = ClassSituationService.INSTANCE.findById(this.id);
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		REQUEST().setAttribute("members", OfferingMemberService.INSTANCE.findByOfferingId(this.offeringId));
		REQUEST().setAttribute("offering", offering);
		REQUEST().setAttribute("model", situation);
		return SUCCESS;
	}
	
	public String editClassSituation() {
		ClassSituation situation = ClassSituationService.INSTANCE.findById(this.id);
		OfferingMember member = OfferingMemberService.INSTANCE.findById(this.memberId);
		situation.setOfferingMember(member);
		situation.setDate(DateUtil.fromString(this.date, this.i18n("format.date")));
		situation.setSituation(this.situation);
		situation.setComment(this.comment);
		situation.setProgress(this.progress);
		
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		situation.setOffering(offering);
		
		situation = ClassSituationService.INSTANCE.persist(situation);		
		return SUCCESS;
	}
	
	public String deleteClassSituation() {
		boolean ret = ClassSituationService.INSTANCE.deleteById(this.id);
		if(ret) {
			return SUCCESS;
		}
		return ERROR;		
	}	

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getTeacher() {
		return teacher;
	}

	public void setTeacher(String teacher) {
		this.teacher = teacher;
	}

	public String getMethodology() {
		return methodology;
	}

	public void setMethodology(String methodology) {
		this.methodology = methodology;
	}

	public String getSkills() {
		return skills;
	}

	public void setSkills(String skills) {
		this.skills = skills;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Long getOfferingId() {
		return offeringId;
	}

	public void setOfferingId(Long offeringId) {
		this.offeringId = offeringId;
	}

	public Long getMemberId() {
		return memberId;
	}

	public void setMemberId(Long memberId) {
		this.memberId = memberId;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getSituation() {
		return situation;
	}

	public void setSituation(String situation) {
		this.situation = situation;
	}

	public String getProgress() {
		return progress;
	}

	public void setProgress(String progress) {
		this.progress = progress;
	}
}
